package net.ssta.common;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.ssta.core.db.DBConnectionProvider;
import net.ssta.core.db.DataBaseExecutor;
import net.ssta.core.db.RecordSet;

import org.apache.log4j.Logger;

import com.yztc.rc.api.framework.Context;
import com.yztc.rc.api.framework.ContextFactory;
import com.yztc.rc.api.framework.entity.UserAccount;

public class LoginUser {
	private static Logger log = Logger.getLogger(LoginUser.class.getClass()); 
	
	/**
	 * 得到当前登录人的账号
	 * @return
	 */
	public static String getUserAccount(){
		String username=null;
		try{
			Context context=ContextFactory.getContext();
			username=context.getUserAccount().getLoginName();
		}catch(Exception e){
			return null;
		}
		return username;
	}
	
	
	/**
	 * 得到当前登录人的ID
	 * @return
	 */
	public static String getUserId(){
		String userId=null;
		try{
			Context context=ContextFactory.getContext();
			userId=context.getUserAccount().getUserId();
		}catch(Exception e){
			return null;
		}
		return userId;
	}
	
	
	/**
	 * 得到当前登录人的ID
	 * @return
	 */
	public static String getUserName(){
		String usrName=null;
		try{
			Context context=ContextFactory.getContext();
			usrName=context.getUserAccount().getUserName();
		}catch(Exception e){
			return null;
		}
		return usrName;
	}
	
	/**
	 * 得到当前登录人的ID
	 * @return
	 */
	public static String getLoginName(){
		String usrName=null;
		try{
			Context context=ContextFactory.getContext();
			usrName=context.getUserAccount().getLoginName();
		}catch(Exception e){
			return null;
		}
		return usrName;
	}
	
	/**
	 * 得到当前登录人的ID
	 * @return
	 */
	public static String getCreateTime(){
		try{
			Context context=ContextFactory.getContext();
			Date date=context.getUserAccount().getCreateTime();
			DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			return df.format(date);
		}catch(Exception e){
			return null;
		}
	}
	
	
	/**
	 * 得到当前登录人的ID
	 * @return
	 */
	public static String getIndustry(){
		try{
			Context context=ContextFactory.getContext();
			String industry=context.getUserAccount().getQuestion();
			if(industry==null||"".equals(industry)||"null".equals(industry)){
				initUa();
			}
			
			return context.getUserAccount().getQuestion();
		}catch(Exception e){
			return null;
		}
	}
	
	
	/**
	 * 1、是外部用户 0是内部用户
	 * @return
	 */
	public static String getUserType(){
		try{
			Context context=ContextFactory.getContext();
			String industry=context.getUserAccount().getQuestionPwd();
			if(industry==null||"".equals(industry)||"null".equals(industry)){
				initUa();
			}
			return context.getUserAccount().getQuestionPwd();
		}catch(Exception e){
			return null;
		}
	}
	
	/**
	 * 空是普通用户  0、是机构用户 1是专家用户
	 * @return
	 */
	public static String getUserUpgradeType(){
		try{
			Context context=ContextFactory.getContext();
			String industry=context.getUserAccount().getDescription();
			if(industry==null||"".equals(industry)){
				initUa();
			}
			return context.getUserAccount().getDescription();
		}catch(Exception e){
			return null;
		}
	}
 
	/**
	 * 得到用户升级后的外键ID，如专家ID，机构ID
	 * @return
	 */
	public static String getUserUpgradeId(){
		try{
			Context context=ContextFactory.getContext();
			String industry=context.getUserAccount().getUpdateUser();
			if(industry==null||"".equals(industry)){
				initUa();
			}
			return context.getUserAccount().getUpdateUser();
		}catch(Exception e){
			return null;
		}
	}
	/**
	 * 得到当前登录人的ID
	 * @return
	 */
	public static void initUa(){
		Connection conn=null;
		try{
			 String userId=getUserId();
			 StringBuffer sb = new StringBuffer();
		     sb.append(" SELECT T.OPTIONVALUE,T.OPTIONNAME,U.NEITYPE,U.LoginName,up.FKTYPE,up.FKID FROM  V_REGISTER_USER U ");
		     sb.append(" left join T_FIELDOPTION T on U.INDUSTRY=T.OPTIONVALUE AND T.TABLENAME='common' AND T.FIELDNAME='NewEnergyClass' ");
		     sb.append(" left join T_USERUPGRADE up on u.USERID=up.USERID and up.FKID IS NOT NULL ");
		     
		     sb.append(" WHERE U.USERID='").append(userId).append("'");
		     
		     
		     conn = DBConnectionProvider.getConnection("jdbc/SSTA");
		     DataBaseExecutor dbe = DataBaseExecutor.getExecutor(conn);
		     RecordSet rs=dbe.find(sb.toString());
		     if(rs==null||rs.size()<1){
		    	 return;
		     }
		     Context context=ContextFactory.getContext();
		     String industryCn= rs.get(0).getString("OPTIONNAME","未知");
		     String userType= rs.get(0).getString("NEITYPE","1");
		     context.getUserAccount().setQuestion(industryCn);
		     context.getUserAccount().setQuestionPwd(userType);
		     context.getUserAccount().setDescription(rs.get(0).getString("FKTYPE",""));
		     context.getUserAccount().setUpdateUser((rs.get(0).getString("FKID","")));
		} catch (SQLException e) {
			log.error("查找用户扩展属性出错："+e);
		 
		}finally{
			try{
				if(conn != null) conn.close();
			}catch (SQLException e){
				 log.error("关闭conn出错",e);
			}
		}
			
	}


}
